import { dialog, clipboard } from 'electron';
import { ctl } from '../global';

/**
 * 指令管理器
 *
 * @author chitanda
 * @date 2022-06-08 17:06:15
 * @export
 * @class CommandHelper
 */
export class CommandHelper {
  /**
   * 登录
   *
   * @author chitanda
   * @date 2022-06-09 17:06:26
   * @return {*}  {Promise<void>}
   */
  async login(): Promise<void> {
    await ctl.openLoginWindow()
  }

  /**
   * 临时从粘贴板中获取 token
   *
   * @author chitanda
   * @date 2022-06-08 18:06:33
   * @protected
   * @return {*}  {(Promise<string | null>)}
   */
  protected async getToken(): Promise<string | null> {
    const token = clipboard.readText();
    const res = await dialog.showMessageBox({
      type: 'info',
      title: '粘贴板获取认证',
      message: `请确认以下信息是否为 Token：\n\n${token}`,
      buttons: ['确定', '重新读取', '取消'],
    });
    switch (res.response) {
      case 0:
        return token;
      case 1:
        return this.getToken();
    }
    return null;
  }
}
// 导出为全局指令
export const command = new CommandHelper();
